package com.nbsaas.boot.data.rest.resource;

import com.nbsaas.boot.data.api.apis.QueryCatalogApi;
import com.nbsaas.boot.data.data.entity.QueryCatalog;
import com.nbsaas.boot.data.api.domain.request.QueryCatalogRequest;
import com.nbsaas.boot.data.api.domain.response.QueryCatalogResponse;
import com.nbsaas.boot.data.api.domain.simple.QueryCatalogSimple;
import com.nbsaas.boot.data.rest.convert.QueryCatalogSimpleConvert;
import com.nbsaas.boot.data.rest.convert.QueryCatalogEntityConvert;
import com.nbsaas.boot.data.rest.convert.QueryCatalogResponseConvert;
import com.nbsaas.boot.data.data.repository.QueryCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.data.api.domain.request.QueryCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   -业务接口实现
*/
@Transactional
@Service
public class QueryCatalogResource extends BaseResource<QueryCatalog,QueryCatalogResponse, QueryCatalogSimple, QueryCatalogRequest>  implements QueryCatalogApi {

    @Resource
    private QueryCatalogRepository queryCatalogRepository;

    @Override
    public JpaRepositoryImplementation<QueryCatalog, Serializable> getJpaRepository() {
        return queryCatalogRepository;
    }

    @Override
    public Function<QueryCatalog, QueryCatalogSimple> getConvertSimple() {
        return new QueryCatalogSimpleConvert();
    }

    @Override
    public Function<QueryCatalogRequest, QueryCatalog> getConvertForm() {
        return new QueryCatalogEntityConvert();
    }

    @Override
    public Function<QueryCatalog, QueryCatalogResponse> getConvertResponse() {
        return new QueryCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<QueryCatalogSimple> list(PageRequest request) {
        QueryCatalogSimpleConvert convert=new QueryCatalogSimpleConvert();
        if (request instanceof QueryCatalogSearch){
             QueryCatalogSearch searchRequest=(QueryCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<QueryCatalogSimple> root(QueryCatalogSearch search) {
        ListResponse<QueryCatalogSimple> result=new  ListResponse<>();
        List<QueryCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


